package com.dp.temp;

import com.dp.io.ESN;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;



@RestController
@RequestMapping("wordHd")
public class excel_word {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    //生成排查结果清单
    @GetMapping(value = "wordRwZt")
    public void wordRwZt(HttpServletResponse response) {
        try {
            Workbook workbook = wordRwZt(findRwZt(), "document/wordRwZt.xlsx");
            OutputStream out = response.getOutputStream();
            response.setHeader("Content-Type", "application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment;filename=风险隐患排查结果.xmls");
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("UTF-8");
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static Workbook wordRwZt(List<Map<String,Object>> map, String path) throws IOException {
        Resource resource = new ClassPathResource(path);
        Workbook workbook = new XSSFWorkbook(resource.getInputStream());
        int count = 5;
        int rcount = 4;
        for(int i = 0; i < map.size();i++){
            if(i != map.size()-1){
                ReadAndCreateExcel.copyRows(5,5,count,((XSSFWorkbook) workbook).getSheetAt(0));
            }
            Row row = workbook.getSheetAt(0).getRow(rcount);
            if(row != null){
                if ("0".equals(map.get(i).get("STATUS").toString())){
                    row.getCell(0).setCellValue("未完成");
                }else {
                    row.getCell(0).setCellValue("已完成");
                }
                row.getCell(1).setCellValue(map.get(i).get("ONAME").toString());
                row.getCell(2).setCellValue(map.get(i).get("RISK_UNIT_NAME").toString());
                String a = map.get(i).get("WORK_TYPE").toString();
                if ("0".equals(a)){
                    row.getCell(3).setCellValue("日常任务");
                }else if ("1".equals(a)){
                    row.getCell(3).setCellValue("主要负责人任务");
                }else if ("2".equals(a)){
                    row.getCell(3).setCellValue("技术负责人任务");
                }else if ("3".equals(a)){
                    row.getCell(3).setCellValue("操作负责人任务");
                }
                row.getCell(4).setCellValue(map.get(i).get("TROUBLESHOOT_CONTENT").toString());
                row.getCell(5).setCellValue(map.get(i).get("DNAME").toString());
                row.getCell(6).setCellValue(map.get(i).get("PNAME").toString());
                row.getCell(7).setCellValue(map.get(i).get("UNAME").toString());
                row.getCell(8).setCellValue(map.get(i).get("CREATE_DATE").toString());
                row.getCell(9).setCellValue(map.get(i).get("endtime").toString());
                row.getCell(10).setCellValue(map.get(i).get("INVESTIGATION_DATE").toString());
            }
            rcount++;
            count++;
        }
        return workbook;
    }

    public List<Map<String, Object>> findRwZt() {
        String sql = " SELECT b.ONAME, b.RISK_UNIT_NAME, a.RWID, b.TROUBLESHOOT_CONTENT, a.INVESTIGATION_DATE, a.STATUS, a.endtime, a.CREATE_DATE, c.CREATE_BY, a.UNAME, a.DNAME, a.PNAME, b.WORK_TYPE FROM hd_check_task a LEFT JOIN hd_check c ON a.RWID = c.RWID LEFT JOIN hd_check_plain b ON a.PID = b.ID WHERE b.WORK_TYPE != '0' ORDER BY CREATE_DATE desc ";
        return jdbcTemplate.queryForList(sql);
    }
}
